package net.ibizsys.eclipse.studio.xml;

import java.util.ArrayList;
import java.util.List;

import net.ibizsys.eclipse.studio.util.StringKit;

import org.w3c.dom.Node;

/**
 * 视图集合
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class IBizViewList extends XmlNodeList<IBizView>
{
	private static final long serialVersionUID = 1L;
	public static final String NODE_PSAPPVIEWS = "PSAPPVIEWS";

	@Override
	protected void initChildNode(Node node, IBizProject project)
	{
		if (StringKit.IsEqual(node.getNodeName(), IBizDEView.NODE_PSAPPDEVIEW, true))
		{
			IBizView ibizView = new IBizDEView();
			ibizView.init(node, project);

			add(ibizView);

			return;
		}
		if (StringKit.IsEqual(node.getNodeName(), IBizIndexView.NODE_PSAPPINDEXVIEW, true))
		{
			IBizView ibizView = new IBizIndexView();
			ibizView.init(node, project);

			add(ibizView);

			return;
		}

		super.initChildNode(node, project);
	}

	public List<IBizView> listAppViews()
	{
		List<IBizView> views = new ArrayList<IBizView>();

		for (int i = 0; i < size(); i++)
		{
			IBizView v = get(i);

			if (v instanceof IBizIndexView)
			{
				views.add(v);
			}
		}

		return views;
	}

	public List<IBizView> listDEViews()
	{
		List<IBizView> views = new ArrayList<IBizView>();

		for (int i = 0; i < size(); i++)
		{
			IBizView v = get(i);

			if (v instanceof IBizDEView)
			{
				views.add(v);
			}
		}

		return views;
	}

	public List<IBizDEView> listDEViews(String deid)
	{
		List<IBizDEView> views = new ArrayList<IBizDEView>();

		for (int i = 0; i < size(); i++)
		{
			IBizView v = get(i);

			if (v instanceof IBizDEView && StringKit.IsEqual(((IBizDEView) v).getDEId(), deid, true))
			{
				views.add((IBizDEView) v);
			}
		}

		return views;
	}

	public List<IBizView> match(List<String> entities, String[] keywords)
	{
		List<IBizView> list = new ArrayList<IBizView>();

		for (IBizView entity : this)
		{
			if (entity instanceof IBizDEView)
			{
				IBizDEView deView = (IBizDEView) entity;

				if (!entities.contains(deView.getDEId()))
				{
					continue;
				}
			}
			
			if (entity.match(keywords))
			{
				list.add(entity);
			}
		}

		return list;
	}

	public List<IBizView> match(List<String> entities, String keyword)
	{
		List<IBizView> list = new ArrayList<IBizView>();

		for (IBizView entity : this)
		{
			if (entity instanceof IBizDEView)
			{
				IBizDEView deView = (IBizDEView) entity;

				if (!entities.contains(deView.getDEId()))
				{
					continue;
				}
			}

			if (entity.match(keyword))
			{
				list.add(entity);
			}
		}

		return list;
	}

}
